const { CreateArticle, selectArticle, deleteArticle } = require('../services/PostService')

class PostController {

    async PostArticle(ctx, next) {
        const { user_id: authorId, user_nickname: authorName, image } = ctx.state.user
        const { contentImage, content } = ctx.request.body
        const res = await CreateArticle({ authorId, authorName, contentImage, content, image })
        if (res) {
            return ctx.body = {
                code: 0,
                message: "success",
                result: "发布成功"
            }
        }

    }

    async PostList(ctx, next) {
        const { pageNo, pageSize } = ctx.request.query
        const res = await selectArticle({ pageNo, pageSize })
        ctx.body = {
            code: 0,
            message: 'success',
            res
        }


    }

    async deleteList(ctx, next) {
        const { id } = ctx.request.query
        const { user_id: authorId } = ctx.state.user
        const res = await deleteArticle({ id, authorId })
        if (res > 0) {
            ctx.body = {
                code: 0,
                message: 'success',
                rsult: '删除成功'
            }
        }

    }
}

module.exports = new PostController()